﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using Asteroids.Game.PowerUps;


namespace Asteroids.Game.PowerUps
{
    public abstract class PowerUp : ICollide, IRender, ICloneable
    {
        public PowerUpType Type { get; set; }
        public BitmapImage Img { get; set; }
        public bool Collected { get; set; }

        private const int POWERUP_WIDTH = 50;
        private const int POWERUP_HEIGHT = 50;

        protected double PowerUpProbability = 0.0;

        protected PowerUp(Coordinate location)
        {
            Location = location;
        }

        public bool IsWithinOf(double range, ICollide collidable)
        {
            return range > Math.Sqrt(Math.Pow(Location.X - collidable.Location.X, 2) +
                                     Math.Pow(Location.Y - collidable.Location.Y, 2));
        }

        public Canvas Render(RenderEventArgs e)
        {
            var retCanvas = new Canvas
                                {
                                    Width = POWERUP_WIDTH,
                                    Height = POWERUP_HEIGHT,
                                    Margin = new Thickness(Location.X - Radius, Location.Y - Radius, 0, 0),
                                    Background = new ImageBrush(Img)
                                };

            return retCanvas;
        }

        public bool TestCollideWith(ICollide collidable)
        {
            return IsWithinOf(Radius + collidable.Radius, collidable);
        }

        public void Collide(CollideEventArgs e)
        {
            Collected = true;
            PerformAction((Player) e.Collided);
        }

        public abstract void PerformAction(Player player);

        public bool IsVulnerable()
        {
            return true;
        }

        public Coordinate Location { get; set; }
        public double Radius
        {
            get { return 25; }
            set { ; }
        }

        public abstract object Clone();

        public static PowerUp GetRandomPowerUp(Coordinate location)
        {
            var totalProb = AllPowerUps.Sum(powerUp => powerUp.PowerUpProbability);
            Console.WriteLine("totalProb: " + totalProb);
            var probIndex = Game.Random.NextDouble()*totalProb;
            Console.WriteLine("probIndex: "+ probIndex);
            var index = 0;
            for (index = 0; probIndex > AllPowerUps[index].PowerUpProbability; probIndex -= AllPowerUps[index].PowerUpProbability, index++)
            {
            }
            Console.WriteLine("index: " + index);

            var ret = (PowerUp) (AllPowerUps[index].Clone());
            Console.WriteLine("Type: " + ret.Type);
            ret.Location = location;
            return ret;
        }

        // this is a static reference to instances of all the power up types.
        // if you want to make a new power up, you must add it to this array.
        public static PowerUp[] AllPowerUps = new PowerUp[]
                                                  {
                                                      new Shield(new Coordinate(0, 0)),
                                                      new Nuke(new Coordinate(0, 0)),
                                                      new Laser(new Coordinate(0, 0)),
                                                      new ExtraLife(new Coordinate(0, 0)),
                                                      new TriShot(new Coordinate(0, 0)),
                                                      new BurstShot(new Coordinate(0, 0)),
                                                  };
    }

    public enum PowerUpType
    {
        Shield,
        Nuke,
        Laser,
        ExtraLife,
        BurstShot,
        TriShot
    }
}
